package org.pupil.rpc.router;

import org.pupil.rpc.common.ServiceMeta;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 记录服务节点
 */
public class ServiceMetaRes {

    // 当前服务节点
    private ServiceMeta curServiceMeta;

    // 剩余服务节点，用于容错机制
    private Collection<ServiceMeta> otherServiceMeta;

    // 获取当前服务节点
    public ServiceMeta getCurServiceMeta() {
        return curServiceMeta;
    }

    // 获取剩余服务节点
    public Collection<ServiceMeta> getOtherServiceMeta()  {
        return otherServiceMeta;
    }

    public ServiceMetaRes(){

    }

    // 构建 服务节点记录
    public static ServiceMetaRes build(ServiceMeta curServiceMeta,Collection<ServiceMeta> otherServiceMeta){
        final ServiceMetaRes serviceMetas = new ServiceMetaRes();
        serviceMetas.curServiceMeta = curServiceMeta;
        // 如果只有一个服务
        if (otherServiceMeta.size() == 1){
            // 当只有一个服务时，剩余服务节点为空
            otherServiceMeta = new ArrayList<>();
        }else {
            // 去除当前服务节点
            otherServiceMeta.remove(curServiceMeta);
        }
        serviceMetas.otherServiceMeta = otherServiceMeta;
        return serviceMetas;
    }
}
